package com.xbongbong.pro.statistic.pojo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.Arrays;

/**
 * logStash output配置文件实体类
 * @author zhouwq
 * @date 2019/5/13 10:49
 */
@Getter
@Setter
@ToString
public class LogStashOutputPojo {

    /**
     * #ESIP地址与端口
     */
    private String[] hosts;

    private String  user;
    private String  password;
    /**
     * ES索引名称
     */
    private String index;
    /**
     * 该index下的type值
     */
    private String documentType;
    /**
     * 自增ID编号
     */
    private String documentId;
    /**
     * 路由
     */
    private String routing;

    public LogStashOutputPojo(){
        this.documentId = "%{esId}";
        this.routing = "%{parent}";
    }

    public String getLogStashOutput(){
        return "\tif[type] == \""+ index +"\"{" + "\n" +
                "\t\telasticsearch {\n" +
                "\t\t\thosts => " + formatArray(hosts) + "\n" +
                "\t\t\tuser => " + user + "\n" +
                "\t\t\tpassword => \"" + password + "\"\n" +
                "\t\t\tindex => \"" + index + "\"\n" +
//                "\t\t\tdocument_type => \"" + documentType + "\"\n" +
                "\t\t\tdocument_id => \"" + documentId + "\"\n" +
                "\t\t\trouting => \"" + routing + "\"\n" +
                "\t\t}" +"\n" +
                "\t}";
    }

    public String getLogStashOutputUpdate(){
        return "\tif[type] == \""+ index +"_update\"{" + "\n" +
                "\t\telasticsearch {\n" +
                "\t\t\thosts => " + formatArray(hosts) + "\n" +
                "\t\t\tuser => " + user + "\n" +
                "\t\t\tpassword => \"" + password + "\"\n" +
                "\t\t\taction => \"update\"\n" +
                "\t\t\tindex => \"" + index + "\"\n" +
//                "\t\t\tdocument_type => \"" + documentType + "\"\n" +
                "\t\t\tdocument_id => \"" + documentId + "\"\n" +
                "\t\t\trouting => \"" + routing + "\"\n" +
                "\t\t}" +"\n" +
                "\t}";
    }

    private static String formatArray(String[] hosts){
        StringBuilder result = new StringBuilder();
        result.append("[");
        for(String str : hosts){
            result.append("\"").append(str).append("\"").append(",");
        }
        return result.substring(0,result.length()-1)+"]";
    }

    public String[] getHosts() {
        if (hosts == null) {
            return null;
        }else{
            return hosts.clone();
        }
    }

    public void setHosts(String[] hosts) {
        if (hosts == null)
        {
            this.hosts = null;
        } else {
            this.hosts = hosts.clone();
        }
    }
}
